package Admin.dao.impl;

import Admin.dao.AdminDao;
import Admin.dao.BaseDao;
import Admin.entity.Admin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenle
 * @version 1.0
 * @date 2023/9/20 19:33
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public int update(Admin admin) {
        String sql="UPDATE `BurgerKingDB`.`Administrator` SET  `authentication` = 0 WHERE `id` = ?;";
        return this.executeUpdate(sql,admin.getId());
    }
    @Override
    public int updateTop(Admin admin) {
        String sql="UPDATE `BurgerKingDB`.`Administrator` SET  `authentication` = 1 WHERE `id` = ?;";
        return this.executeUpdate(sql,admin.getId());
    }
    @Override
    public List<Admin> getLike(String name) {
        String sql="SELECT * FROM Administrator WHERE AdminName LIKE '%"+name+"%'";
        ResultSet rs=this.executeQuery(sql);
        List<Admin> list=new ArrayList<>();
        try {
            while (rs.next()) {
                list.add(new Admin(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7),rs.getDouble(8)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }



    @Override
    /**
     * 管理员信息列表
     */
    public List<Admin> getlist() {
        String sql="SELECT * FROM Administrator";
        ResultSet rs=this.executeQuery(sql);
        List<Admin> list=new ArrayList<>();
        try {
            while (rs.next()){
                list.add(new Admin(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7),rs.getDouble(8)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }
    @Override
    public int delete(Admin admin) {
        String sql="DELETE FROM Administrator WHERE id=?";
        return this.executeUpdate(sql,admin.getId());
    }

    @Override
    public int updatedalance(Admin admin) {
        String sql="UPDATE `BurgerKingDB`.`Administrator` SET  `balance` =? WHERE `id` = 11;";
        return this.executeUpdate(sql,admin.getBalance());
    }

    @Override
    public double selectDalance() {
        String sql="select balance from Administrator where id=11";
        ResultSet rs=this.executeQuery(sql);
        double ind=0.00;
        try {
            while (rs.next()){
                ind=rs.getDouble(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return ind;
    }
}
